// 处理动态路由：
import type { Router, RouteRecordRaw } from "vue-router";

// 基础路由
export const baseRoutes = [
    {
        path: "/",
        redirect: "/Login"
    },
    {
        path: "/Login",
        name: "Login",
        component: () => import("@/views/Login.vue"),
    },
    {
        path: "/Admin",
        name: "Admin",
        component: () => import("@/views/Admin/Layout/Layout.vue"),
        children: [
            {
                path: "PiniaDemo",
                component: () => import("@/views/Admin/PiniaDemo.vue")
            }
        ]
    }
]


//  权限路由：
const grantRoutes = [
    {
        key: "1",
        path: "",
        name: "AdminIndex",
        component: () => import("@/views/Admin/Index.vue")
    },
    {
        key: "2-1",
        path: "BannerList",
        name: "AdminBannerList",
        component: () => import("@/views/Admin/Banner/BannerList.vue")
    },
    {
        key: "2-2",
        path: "AddBanner",
        name: "AdminAddBanner",
        component: () => import("@/views/Admin/Banner/AddBanner.vue")
    },
    {
        key: "3-1",
        path: "AdminList",
        name: "AdminAdminList",
        component: () => import("@/views/Admin/Admin/AdminList.vue")
    },
    {
        key: "3-2",
        path: "AddAdmin",
        name: "AdminAddAdmin",
        component: () => import("@/views/Admin/Admin/AddAdmin.vue")
    },
    {
        key: "4-1",
        path: "GoodsList",
        name: "AdminGoodsList",
        component: () => import("@/views/Admin/Goods/GoodsList.vue")
    },
    {
        key: "4-2",
        path: "SearchGoods",
        name: "AdminSearchGoods",
        component: () => import("@/views/Admin/Goods/SearchGoods.vue")
    },
    {
        key: "5",
        path: "MyMap",
        name: "AdminMyMap",
        component: () => import("@/views/Admin/MyMap.vue")
    },
    {
        key: "6",
        path: "PiniaDemo",
        name: "AdminPiniaDemo",
        component: () => import("@/views/Admin/PiniaDemo.vue")
    }
]


// 根据后端返回的权限（checkedKeys）和权限路由，产生当前用户的路由。
export function getCurrentRoutes(router: Router) {
    let checkedKeys = JSON.parse(sessionStorage.getItem("checkedKeys") as string);//["1","2","2-1","2-2"]

    if (checkedKeys == null) {
        return
    }

    if (checkedKeys.length == 0) {//如果是超级管理员
        checkedKeys = ["1", "2", "2-1", "2-1-R", "2-2", "3", "3-1", "3-2", "4", "4-1", "4-2", "5", "6"];
    }

    checkedKeys && grantRoutes.forEach(item => {
        if (checkedKeys.includes(item.key)) {
            // 给路由对象动态增加路由（使用路由对象的addRoute方法）；

            let crud = "";//crud分别表示 添加，读，修改，删除。分别用0和1表示是否具备crud权限。

            if (checkedKeys.includes(item.key + "-C")) { //2-1-C
                crud += "1";
            } else {
                crud += "0";
            }
            if (checkedKeys.includes(item.key + "-R")) {//2-1-R
                crud += "1";
            } else {
                crud += "0";
            }
            if (checkedKeys.includes(item.key + "-U")) {
                crud += "1";
            } else {
                crud += "0";
            }
            if (checkedKeys.includes(item.key + "-D")) {
                crud += "1";
            } else {
                crud += "0";
            }

            item.meta = {
                crud: crud
            }
            router.addRoute("Admin", item);
        }
    })

}